package com.aliyun.messagemgt.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.messagemgt.application.service.MsgSourceService;
import com.aliyun.messagemgt.application.service.sms.SmsStatisticsService;
import com.aliyun.messagemgt.util.SmsQuotaDateUtils;
import com.aliyun.messagemgt.common.enums.ChannelTypeEnum;
import com.aliyun.messagemgt.dto.SmsQuotaUsed;
import com.aliyun.messagemgt.dto.query.MsgQuery;
import com.aliyun.messagemgt.dto.vo.SmsTrendQueryVO;
import com.aliyun.messagemgt.dto.vo.SmsTrendResultVO;
import com.aliyun.messagemgt.domain.repository.SmsQuotaUsedRepository;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author byp
 * @date 2020/7/31
 */
@Api(tags = "短信统计概览接口")
@CrossOrigin
@RestController
@RequestMapping("/quota/statistics")
public class SmsQuotaStatisticsController {

    @Autowired
    private SmsQuotaUsedRepository smsQuotaUsedMapper;
    @Autowired
    private SmsStatisticsService smsStatisticsService;
    @Autowired
    private MsgSourceService msgSourceService;

    @ApiOperation(value = "获取累计推送量")
    @PostMapping("/pushStatistics")
    @ResponseBody
    public Object pushStatistics() {

        List<SmsQuotaUsed> usedList = smsQuotaUsedMapper.querySmsQuotaUsedByEntity(new SmsQuotaUsed());
        //累计推送量
        int cumulativeCount = usedList.stream().mapToInt(SmsQuotaUsed::getUsedQuota).sum();

        //今年推送量
        int yearCount = usedList.stream().filter(used ->
                StringUtils.equals(SmsQuotaDateUtils.currentYear(), used.getUsedYear())
        ).mapToInt(SmsQuotaUsed::getUsedQuota).sum();

        //本月推送量
        int monthCount = usedList.stream().filter(used ->
                StringUtils.equals(SmsQuotaDateUtils.currentYear(), used.getUsedYear()) &&
                        StringUtils.equals(SmsQuotaDateUtils.currentMonth(), used.getUsedMonth())
        ).mapToInt(SmsQuotaUsed::getUsedQuota).sum();

        JSONObject result = new JSONObject();
        result.put("cumulativeCount", cumulativeCount);
        result.put("yearCount", yearCount);
        result.put("monthCount", monthCount);
        return result;
    }

    @ApiOperation(value = "获取所有的短信消息源")
    @PostMapping("/smsMsgSourceList")
    @ResponseBody
    public Object smsMsgSourceList() {
        //暂时不过滤审批状态
        MsgQuery msgQuery = new MsgQuery();
        msgQuery.setPushChannelType(ChannelTypeEnum.DYSMS.getType());
        return msgSourceService.smsMsgSourceList(msgQuery);
    }

    /**
     * 查询周期内的趋势
     * 1.默认展示最近15天全部短信渠道消息源的推送数据。包括推送成功条数、推送失败条数
     * 2.可以查看全部短信渠道消息源的最近15、最近30天、最近12月、自定义日期推送数量。
     * 3.可以选择某一短信渠道消息源的最近15、最近30天、最近12月、自定义日期推送数量。
     * 4.自定义时间可选择的最大时间范围是365天。当选择的天数小于等于90天时，按照天数展示曲线图。当选择的天数大于90天时，按照月份展示曲线图内容。
     *
     * @param smsTrendQueryVO 查询入参
     * @return 数据
     */
    @ApiOperation(value = "查询周期内的趋势")
    @PostMapping("/trend")
    @ResponseBody
    @ApiResponses(
            @ApiResponse(code = 200, message = "success", response = SmsTrendResultVO.class)
    )
    public Object trend(@ApiParam(value = "短信配额列表查询参数") @RequestBody SmsTrendQueryVO smsTrendQueryVO) {
        return smsStatisticsService.trend(smsTrendQueryVO);
    }

}
